package com.atguigu.gmall.product.rpc.controller;


import com.atguigu.gmall.common.constant.RedisConstant;
import com.atguigu.gmall.model.product.SkuImage;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.product.service.SkuImageService;
import com.atguigu.gmall.product.service.SkuInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.redisson.api.RList;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@RequestMapping("/api/inner")
@RestController
public class SkuApiController {


    @Autowired
    SkuInfoService skuInfoService;

    @Autowired
    SkuImageService skuImageService;

    @Autowired
    RedissonClient redissonClient;

    /**
     * 批量获取指定商品的最新价格
     * Map<Long ,Decima>
     *     52: 1999
     *     53: 1799
     * @param skuIdList
     */
    @PostMapping("/batch/getPrice")
    public Map<Long,BigDecimal> batchGetSkuPrice(@RequestBody List<Long> skuIdList){
        Map<Long,BigDecimal> prices = skuInfoService.batchGetSkuPrice(skuIdList);
        return prices;
    }


    @GetMapping("/tmpsave/skuids")
    public String getAllSkuIds(){
        System.out.println("正在重建布隆....");
        //0、查出所有商品的id集合，准备让别人进行布隆重建
        List<Long> ids = skuInfoService.getAllSkuIds();
//
        //1、得到分布式集合对象
        RList<Long> list = redissonClient.getList(RedisConstant.SKUIDS_TEMP_LIST);

        //2、把查到的数据存到redis中
        list.addAll(ids);
        return "ok";
    }


    /**
     * 获取商品sku的info
     * @param skuId
     * @return
     */
    @GetMapping("/skuinfo/{skuId}")
    public SkuInfo getSkuInfo(@PathVariable("skuId")Long skuId){

        return skuInfoService.getById(skuId);
    }


    @GetMapping("/skuinfo/images/{skuId}")
    public List<SkuImage> skuImageList(@PathVariable("skuId")Long skuId){

        QueryWrapper<SkuImage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("sku_id",skuId);
        List<SkuImage> images = skuImageService.list(queryWrapper);
        return images;
    }


    /**
     * 查价格
     * @param skuId
     * @return
     */
    @GetMapping("/sku/price/{skuId}")
    public BigDecimal getSkuPrice(@PathVariable("skuId")Long skuId){
        //查询价格
        BigDecimal price = skuInfoService.getSkuPrice(skuId);
        return price;
    }
}
